package com.jiangfeixiang.springbootblog.controller.admin;

import com.jiangfeixiang.springbootblog.common.ResponseCode;
import com.jiangfeixiang.springbootblog.common.ResponseData;
import com.jiangfeixiang.springbootblog.entity.Users;
import com.jiangfeixiang.springbootblog.service.UsersService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author: 姜飞祥
 * @Description: 用户注册
 * @Date: Create in 2019/1/26/0026 17:07
 * @param: $params$
 * @return: $returns$
 */
@RequestMapping("/admin")
@Controller
public class UserRegisterController {
    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private UsersService usersService;

    /**
     * 跳转到注册页面
     * @return
     */
    @RequestMapping("/registerPage")
    public String register(){

        return "admin/registerPage";
    }

    /**
     * 校验用户名是否存在
     * @param users
     * @return
     */
    @RequestMapping("/checkUsername")
    @ResponseBody
    public ResponseData checkUsername(Users users){
        int iusername = usersService.checkUsername(users);
        if (iusername>0){
            return ResponseData.fail("用户名已存在，请登录");
        }
        return ResponseData.success(ResponseCode.SUCCESS.getCode(),"用户名可以使用");
    }
    /**
     * 校验邮箱是否存在
     * @param users
     * @return
     */
    @RequestMapping("/checkEmail")
    @ResponseBody
    public ResponseData checkEmail(Users users){
        int iemail = usersService.checkEmail(users);
        if (iemail>0){
            return ResponseData.fail("邮箱已经存在");
        }
        return ResponseData.success(ResponseCode.SUCCESS.getCode(),"邮箱可以使用");
    }

    /**
     * 注册
     * @param users
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public ResponseData register(Users users){
        int iregister = usersService.register(users);
        if (iregister > 0){
            return ResponseData.success(ResponseCode.SUCCESS.getCode(),ResponseCode.SUCCESS.getDesc());
        }
        return ResponseData.fail(ResponseCode.SUCCESS.getCode(),ResponseCode.ERROR.getDesc());
    }
}
